package com.masq.aboard.model;

import com.masq.aboard.common.constants.AboardConstants;
import com.masq.aboard.entity.ManagerUserEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * @author masq
 * @since 2024-05-02
 */
public class UserModel implements UserDetails {

    private ManagerUserEntity managerUserEntity;

    public UserModel(ManagerUserEntity managerUserEntity) {
        this.managerUserEntity = managerUserEntity;
    }

    public ManagerUserEntity getManagerUser() {
        return managerUserEntity;
    }

    public void setManagerUser(ManagerUserEntity managerUserEntity) {
        this.managerUserEntity = managerUserEntity;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    public String getPassword() {
        return managerUserEntity.getPassword();
    }

    @Override
    public String getUsername() {
        return managerUserEntity.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !managerUserEntity.isLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        Long expireTime = managerUserEntity.getExpireTime();
        if (expireTime != null) {
            if (AboardConstants.NEVER_EXPIRE.equals(expireTime)) {
                return true;
            }
            return System.currentTimeMillis() < expireTime;
        }
        return false;
    }

    @Override
    public boolean isEnabled() {
        return managerUserEntity.isEnabled();
    }
}
